import time
import cv2
import numpy as np
import pyautogui
import pyperclip

# 读取目标图片
target_image = cv2.imread('red.png')
target_height, target_width, _ = target_image.shape

from langchain_openai import OpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationSummaryMemory
from langchain_community.callbacks import get_openai_callback
import os

# 设置代理
os.environ["http_proxy"] = "http://localhost:7890"
os.environ["https_proxy"] = "http://localhost:7890"

# 初始化模型
llm = OpenAI(
    temperature=0,
    openai_api_key=os.environ.get("OPENAI_API_KEY3.5"),
    model_name="gpt-3.5-turbo-instruct"
)

# 初始化chain
conversation = ConversationChain(llm=llm, memory=ConversationSummaryMemory(llm=llm))


def track_tokens_usage(chain, query):
    with get_openai_callback() as cb:
        result = chain.invoke(query)
        print(f'Total tokens: {cb.total_tokens}')
    return result


# 设置微信聊天框的屏幕区域
wechat_region = (398, 1358, 1170, 1198)  # 左上角和右下角坐标

while True:

    # 截取屏幕截图
    screenshot = pyautogui.screenshot()
    screenshot = np.array(screenshot)
    screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)

    # 在屏幕截图中搜索目标图片
    result = cv2.matchTemplate(screenshot, target_image, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    # 如果匹配度超过阈值，则点击该位置
    if max_val >= 0.6:
        target_x, target_y = max_loc[0] + target_width // 2, max_loc[1] + target_height // 2
        pyautogui.click(target_x, target_y)

        x1, y1 = 495, 1234
        pyautogui.doubleClick(x1, y1)
        pyautogui.hotkey('ctrl', 'c')
        text = pyperclip.paste()

        # 将识别出的文本作为问题输入到对话模型中，并获取响应
        response = track_tokens_usage(conversation, text + "(请用中文简洁回答)")
        x2, y2 = 438, 1359
        pyautogui.click(x2, y2)
        # 响应
        pyperclip.copy(str(response))

        #模拟键盘粘贴操作
        pyautogui.hotkey('ctrl', 'v')

        # Enter发送消息
        pyautogui.press('enter')
        print("对话模型的响应：", response)

    time.sleep(1)

